/**
* @depends jquery.ui.js
*/

(function($) {

	$.widget( 'ui.widget_searchbar', {
		
		_init: function() {
		
			var $this = this;
			var timeout = null;
			var input = $('input', this.element);
			input.focus( function() {
				if( $this.objs.ul.hasClass( 'Down' ) ) {
					$this.objs.ul.removeClass( 'Down' );
					return;
				}
				$this.element.addClass( 'Focus' );
				if( !$this.element.hasClass( 'Default' ) ) {
					$this.objs.ul.show();
				}
				else {
					$this.element
						.removeClass( 'Default' )
						.addClass( 'Focus' );
					input.attr( 'value', '' );
				}
				
				var refresh = function() {
					$this.refresh();
					timeout = setTimeout( refresh, $this.options.delay );
				} 
				refresh();
			} );
			
			input.blur( function( e ) {
				if( $this.objs.ul.hasClass( 'Down' ) ) {
					e.preventDefault();
					e.stopImmediatePropagation();
					input.focus();
					return;
				}
				$this.element.removeClass( 'Focus' );
				clearTimeout( timeout );
				$this.objs.ul.hide();
				if( input.attr( 'value' ) ) {
					return;
				}
				$this.element.addClass( 'Default' );
				input.attr( 'value', $this.options.defaultText );
				
			} );
			
			input.keydown( function( e ) {
				if( e.keyCode == $.ui.keyCode.ENTER ) {
					$this.objs.ul.find( '.Selected' ).click();
				}
				if( e.keyCode == $.ui.keyCode.DOWN ) {
					var selected = $this.objs.ul.find( '.Selected' );
					if( selected.length ) {
						selected.widget_searchbar_result( 'select_next' );
					}
					else {
						$this.objs.ul.find( '.SearchResult:first' ).widget_searchbar_result( 'select' );
					}
				}
				if( e.keyCode == $.ui.keyCode.UP ) {
					var selected = $this.objs.ul.find( '.Selected' );
					if( selected.length ) {
						selected.widget_searchbar_result( 'select_previous' );
					}
					else {
						$this.objs.ul.find( '.SearchResult:last' ).widget_searchbar_result( 'select' );
					}
				}
			} );
			
			this.objs = {
				ul: $('ul', this.element),
				all: $('ul li:last > div', this.element),
				span: $('ul li:last span', this.element)
			};
			
			this.objs.ul.mousedown( function() {
				$this.objs.ul.addClass( 'Down' );
			} );
			
			this.objs.all.widget_searchbar_result();
			this.objs.all.find( 'p' ).andSelf().click( function( e ) {
				location.href = 'Results?search=' + encodeURI(input.attr( 'value' ));
				e.stopImmediatePropagation();
			} )
		},
		
		refresh: function() {
			var $this = this;
			var cache = $this.options.cache;
			var current = $this.options.current;
			var input = $('input', this.element);
			var search = input.attr( 'value' );
			
			if( !search ) {
				$this.objs.ul.hide();
				return;
			}
			else {
				$this.objs.ul.show();
			}
			
			// If we are already displaying the right results, do nothing
			if( current.search == search ) {
				return this;
			}
			current.search = search;
			this.objs.span.html( search );
			
			// If we have wrong results, but we have results in cache, display them
			if( cache.refresh[search] ) {
				if( current.results && current.results.length ) {
					current.results.replaceWith( cache.refresh[search] );
				}
				else {
					$this.objs.ul.prepend( cache.refresh[search] );
				}
				current.results = cache.refresh[search];
				current.results.find( '.SearchResult:first' ).widget_searchbar_result( 'select' );
				return this;
			}
			
			// If we don't have anything, get it
			$.wajax( 'Widget_SearchResults', { search: search }, function( json ) {
				if( json.status != 'success' ) {
					return;
				}
				cache.refresh[search] = $(json.content);
				cache.refresh[search].find( '.SearchResult' ).widget_searchbar_result();
				
				if( current.results && current.results.length ) {
					current.results.replaceWith( cache.refresh[search] );
				}
				else {
					$this.objs.ul.prepend( cache.refresh[search] );
				}
				current.results = cache.refresh[search];
				current.results.find( '.SearchResult:first' ).widget_searchbar_result( 'select' );
			} );
			return this;
		},
		
		options: {
			defaultText: 'Search...',
			refreshes: {},
			filters: [],
			cache: {
				refresh: {}
			},
			current: {
				search: '',
				results: ''
			},
			delay: 1000
		}
		
	} );
	
})(jQuery);

